/* 
 * File:   DummyTarget.h
 * Author: jormar
 *
 * Created on 15 de mayo de 2011, 12:21 PM
 */

#ifndef DUMMYTARGET_H
#define	DUMMYTARGET_H

#ifdef	__cplusplus
extern "C" {
#endif

#include <math.h>
#include <stdlib.h>
#include <iostream>
#include <stdio.h>

#include "CSurface.h"
#include "Character.h"
#include "Behavior/Seek.h"
#include "Behavior/Nothing.h"

using namespace std;

class DummyTarget : public Character {
  public:
    SDL_Surface* carita;

    /*
     * Inicializa al bicho
     */
    bool OnInit(Character* target){
      if((surface = CSurface::OnLoad("images/dummy.png",true)) == NULL){
        cout << "Algo salio mal" << endl;
        return false;
      }

      if((carita = CSurface::OnLoad("images/dummy_head.png",true)) == NULL){
        cout << "Algo salio mal" << endl;
        return false;
      }

      /// Posicionamos el personaje
      k = new Kinematic();
      this->width = this->height = 40;
      k->position[0] = 300;
      k->position[1] = 300;
      k->orientation = M_PI;
      k->max_velocity = 0.0001;
      k->max_rotation = 0.001;

      /* Establecemos su comportamiento, y los vamos almacenando en el orden
       * de prioridad deseado
       */

      setBehavior* stB;
      stB->addBehavior(new Nothing(),this,target);
      behaviors.push_back(stB);

      return true;
    }

    /*
     * Pinta al bicho
     */
    void OnRender(SDL_Surface* display){
      CSurface::OnDraw(display, this->surface,
          this->k->position[0],
          this->k->position[1]);

      CSurface::OnDraw(display, this->carita,
          15*sin(k->orientation) +k->position[0]+ width/4+2,
          15*cos(k->orientation) +k->position[1]+ height/4+2);
    }

    /*
     * Aplica comportamiento, actualiza la posicion
     * y anima sprites (proximamente)
     */
    void OnLoop(long time){
      Steering* steering = this->getSteering();
      update(steering, time);
      delete steering;
    }

    /// Eventos
    void OnKeyDown(SDLKey sym, SDLMod mod, Uint16 unicode){
      Kinematic* current = this->getKinematic();
      switch(sym){
        case SDLK_UP:
          cout << "up" << endl;
          current->position[1] -= 10;
          this->setKinematic(current);
          break;
        case SDLK_DOWN:
          cout << "down" << endl;
          current->position[1] += 10;
          this->setKinematic(current);
          break;
        case SDLK_RIGHT:
          cout << "right " << current->position[0];
          current->position[0] += 10;
          this->setKinematic(current);
          cout << " => " << current->position[0] << endl;
          break;
        case SDLK_LEFT:
          cout << "left" << endl;
          current->position[0] -= 10;
          this->setKinematic(current);
          break;
        case SDLK_m:
          cout << "lo lograste " << endl;
        default:
          ;
      }
    }

    void OnWheelButtonUp(int mX, int mY){
      Kinematic* current = this->getKinematic();
      current->orientation += M_PI / 16.0f;
      this->setKinematic(current);
    }

    void OnWheelButtonDown(int mX, int mY){
      Kinematic* current = this->getKinematic();
      current->orientation -= M_PI / 16.0f;
      this->setKinematic(current);
    }

    void OnMButtonDown(int mX, int mY){
      Kinematic* current = this->getKinematic();
      current->position[0] = mX - width/2;
      current->position[1] = mY - height/2;
      this->setKinematic(current);
    }

    void OnMouseMove(int mX, int mY, int relX, int relY, bool Left,bool Right,bool Middle){
      if(Middle) OnMButtonDown(mX, mY);
    }
};

#ifdef	__cplusplus
}
#endif

#endif	/* DUMMYTARGET_H */

